#include <iostream>
#include <cstdlib>
#include <cstdio>

#include <sys/wait.h>
#include <unistd.h>

void Worker()
{
    int *p = NULL;
    int cnt = 10;
    while (cnt)
    {
        printf("I am child process, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt--);
        sleep(1);
    }
}
const int n = 10;

int main()
{
    pid_t id = fork();
    if (id == 0)
    {
        // child
        Worker();
        exit(1);
    }
    else
    {
        // sleep(10);
        //  father
        int status = 0;
        pid_t rid = waitpid(id, &status, 0);
        printf("wait after\n");
        if (rid == id)
        {
            // 我们不能对status整体使用
            printf("wait success, pid: %d, rpid: %d, exit sig: %d, exit code: %d\n", getpid(), rid, status&0x7F, (status>>8)&0xFF);
            if (WIFEXITED(status))
            {
                printf("child process normal quit, exit code : %d\n", WEXITSTATUS(status));
            }
            else
            {
                printf("child process quit except!\n");
            }
        }
    }
    return 0;
}
